package org.vy.dal.dao;

import java.util.List;

import org.vy.dal.QueryManager;

public class VyVidhyarthiDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE VY_ID = m_VyId";
	private static final String FIND_BY_PARAMETER = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String UPDATE = "UPDATE <TABLE> SET VIDHALAYA_NAME = m_VidhyalayaName,BATCH_NO=m_BatchNo,RANK_SCORED=m_RankScore WHERE VY_ID = m_VyId";
	private static final String INSERT = "INSERT INTO <TABLE> (VY_ID,VIDHALAYA_NAME,BATCH_FROM,BATCH_TO,BATCH_NO,RANK_SCORED) VALUES(m_VyId,m_VidhyalayaName,m_BatchFrom,m_BatchTo,m_BatchNo,m_RankScore)";
	private static final String DELETE = "DELETE FROM <TABLE> WHERE VY_ID = m_VyId";

	public static VyVidhyarthiDO findByPK(String key) throws Exception {
		VyVidhyarthiDO dto = new VyVidhyarthiDO();
		dto.setVyId(key);
		dto = (VyVidhyarthiDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}
	@SuppressWarnings("unchecked")
	public static List<VyVidhyarthiDO> findByParam(VyVidhyarthiDO dto)
			throws Exception {

		StringBuilder sb = new StringBuilder(FIND_BY_PARAMETER);
		if (dto.getVyId() != null) {
			System.out.println(dto.getVyId());
			sb.append(" AND VY_ID LIKE '%" + dto.getVyId() + "%' ");
		}
		if (dto.getVidhyalayaName() != null) {
			System.out.println(dto.getVidhyalayaName());
			sb.append(" AND VIDHALAYA_NAME LIKE '%" + dto.getVidhyalayaName()
					+ "%' ");
		}
		if (dto.getBatchNo() != null) {
			System.out.println(dto.getBatchNo());
			sb.append(" AND BATCH_NO LIKE '%" + dto.getBatchNo() + "%' ");

		}
		if (dto.getBatchFrom() != null) {
			System.out.println(dto.getBatchFrom());
			sb.append(" AND BATCH_FROM LIKE '%" + dto.getBatchFrom() + "%' ");
		}
		if (dto.getBatchTo() != null) {
			System.out.println(dto.getBatchTo());
			sb.append(" AND BATCH_TO LIKE '%" + dto.getBatchTo() + "%' ");
		}
		if (dto.getRankScore() != null) {
			System.out.println(dto.getRankScore());
			sb.append(" AND RANK_SCORED LIKE '%" + dto.getRankScore() + "%' ");

		}
		System.out.println(sb.toString());
		return (List<VyVidhyarthiDO>) QueryManager.executeListQuery(dto, sb
				.toString());
	}

	public static VyVidhyarthiDO insert(VyVidhyarthiDO dto) throws Exception {

		dto = (VyVidhyarthiDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyVidhyarthiDO update(VyVidhyarthiDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyVidhyarthiDO delete(String key) throws Exception {
		VyVidhyarthiDO dto = new VyVidhyarthiDO();
		dto.setVyId(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}

}
